﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using WxTenpay.WModel;
using WxTenpay.Common;

namespace WxTenpay.Tenpay
{
    /// <summary>
    /// 授权网站登入获取个人信息
    /// </summary>
    public class asscess_token
    {
        #region 授权登入获取个人信息
        /// <summary>
        /// 授权登入获取个人信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static gerenxinxi gerenxinxi(string token, string openid)
        {
            var url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid + "&lang=zh_CN ";
            var res = Common.HttpMethods.HttpGet(url);
            gerenxinxi geren = JsonConvert.DeserializeObject<gerenxinxi>(res);
            return geren;
        }
        /// <summary>
        /// 使用code获取openid,access_token(网页的access_token)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Getopenid shouquangerenxinxi(string code)
        {
            string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WXconfig.appid + "&secret=" + WXconfig.secret + "&code=" + code + "&grant_type=authorization_code ";
            var res = Common.HttpMethods.HttpGet(url);
            Getopenid getopenid = JsonConvert.DeserializeObject<Getopenid>(res);
            return getopenid;
        }
        /// <summary>
        /// 检验授权凭证（access_token）是否有效
        /// </summary>
        /// <param name="token"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static erroy pandunaccess_token(string token, string openid)
        {
            var url = "https://api.weixin.qq.com/sns/auth?access_token=" + token + "&openid=" + openid;
            var res = Common.HttpMethods.HttpGet(url);
            erroy err = JsonConvert.DeserializeObject<erroy>(res);
            return err;
        }
        /// <summary>
        /// 刷新网页access_token
        /// </summary>
        /// <param name="token"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static Getopenid ShuaXinaccess_token(string appid, string refresh_token)
        {
            var url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + refresh_token;
            var res = Common.HttpMethods.HttpGet(url);
            Getopenid err = JsonConvert.DeserializeObject<Getopenid>(res);
            return err;
        }
        #endregion

        #region  根据openid 获取信息和网页版授权有所区别

        /// <summary>
        /// 根据openid 获取信息和网页版授权有所区别
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        /// 
        public static gerenxinxi GetPerson(string openid, string access_token)
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/user/info?access_token={access_token}&openid={openid}&lang=zh_CN";
            var res = Common.HttpMethods.HttpGet(url);
            var geren = JsonConvert.DeserializeObject<gerenxinxi>(res);
            return geren;
        }
        #endregion

        #region 微信公众号的Token
        #region 获取Token
        /// <summary>
        /// 获取Token
        /// </summary>
        public static string GetToken(string appid, string secret)
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
            var res = Common.HttpMethods.HttpGet(url);
            Log.WriteLogFile(res, "GetToken");
            var obj = res.ToObject();
            //token tk = JsonConvert.DeserializeObject<token>(res);
            return obj["access_token"].ToString();
        }
        #endregion

        /// <summary>
        /// 获取jsapi_ticket
        /// </summary>
        /// <returns></returns>
        public static string getjsapi_ticket(string Asscess)
        {

            var url = $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={Asscess}&type=jsapi";
            var res = Common.HttpMethods.HttpGet(url);
            Log.WriteLogFile(res, "jsapi_ticket");
            var obj = res.ToObject();
            //   token tk = JsonConvert.DeserializeObject<token>(res);
            // return tk.ticket;
            return obj["ticket"].ToString();
        }
        #endregion
    }
}